<template>
  <!-- 园区 -->
  <div>
    <el-row>
      <el-select
        v-model="checkedData"
        :disabled="readonly"
        filterable
        remote
        reserve-keyword
        :placeholder="$t('请输入编号或名称')"
        :remote-method="remoteMethod"
        @change="changeData"
      >
        <el-option
          v-for="item in options"
          :key="item.parkCode"
          :label="item.parkCode + item.parkName"
          :value="item.parkCode"
        >
        </el-option>
      </el-select>
    </el-row>
  </div>
</template>
<script>
import nonStandardService from '@/services/nonStandardService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'park',
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: [Object, String, Number],
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
    type: Number,
  },
  data() {
    return {
      options: [],
      checkedData: null,
      selectData: {},
      readonly: false,
    }
  },
  created() {
    this.readonly =
      Boolean(this.$route.query.readonly) ||
      this.$route.query.contractSource == 1
    if (this.value) {
      this.setData()
    }
    this.remoteMethod('')
  },
  methods: {
    /**远程搜索货品数据 */
    remoteMethod(query) {
      nonStandardService
        .queryParkManageList({
          codeOrName: query,
          tenantId: localStorage.getItem('tenantId'),
        })
        .then((res) => {
          if (res.code == 200) {
            this.options = res.data.list
          }
        })
    },
    setData() {
      this.selectData = JSON.parse(this.value).selectData
      this.options = [JSON.parse(this.value).selectData]
      this.checkedData = JSON.parse(this.value).checkedData
      localStorage.setItem('parkCode', this.selectData.parkCode)
    },
    changeData(val) {
      this.selectData =
        (this.options && this.options.find((it) => it.parkCode == val)) || {}
      this.$emit('input', this.checkedData)
      localStorage.setItem('parkCode', this.selectData.parkCode)
    },
    getData() {
      return {
        value: this.selectData.parkCode + this.selectData.parkName,
        checkedData: this.checkedData,
        selectData: this.selectData,
      }
    },
  },
}
</script>
